package com.kylin.common.weixin.mp.api;

import com.alibaba.fastjson2.JSONObject;
import com.kylin.common.weixin.mp.bean.template.Template;
import com.kylin.common.weixin.util.HttpClientUtils;
import com.kylin.common.weixin.util.WxRequest;

/**
 * 微信模版消息服务
 *
 * @author wuhao
 * @version 1.0 - 2019-03-22
 */
public class TemplateService {

    public static final String SEND_TEMPLATE_URL = "/cgi-bin/message/template/send?access_token=%s";

    public static final String GET_ALL_TEMPLATE_URL = "/cgi-bin/template/get_all_private_template?access_token=%s";

    /**
     * 发送模版消息
     * @param template 模板内容
     * @param token 调用接口凭证
     * @return 发送结果
     * @version 1.0  by wuhao at 2019-03-22
     */
    public static JSONObject send(Template template, String token){
        String url = String.format(SEND_TEMPLATE_URL,token);
        return WxRequest.post(url,template.toJSON().toJSONString());
    }

    /**
     * 发送模版消息
     * @param
     * @param token 调用接口凭证
     * @return 发送结果
     * @version 1.0  by wuhao at 2019-03-22
     */
    public static JSONObject send2(JSONObject json, String token){
        String url = String.format(SEND_TEMPLATE_URL,token);
        return WxRequest.post(url,json.toJSONString());
    }
    /**
     * 获取模板列表
     * @param token
     * @return com.alibaba.fastjson.JSONObject
     * @version 1.0 by ZhuangYuXiang at 2019/4/12
     */
    public static JSONObject list(String token){
        String url = String.format(GET_ALL_TEMPLATE_URL,token);
        return WxRequest.get(url);
    }

    public static void main(String[] args) {
        String token = "20_iK-PaMl2eHuC0KsPb7lY6bt_E2yU7XGCmDckXL-QrD4f9pztwG1iAo7x9F43MR6RgdagJathDhMEXt8dnZ0S1dRx-Sf3iFu7fwfmuzByAm6kIXR1fic-XnTa5OiVXZS6o8fuLKKz2KgK8c3OIVXcADAPCE";
        System.out.println(list(token));

        System.out.println(HttpClientUtils.get("https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token="+token));
    }

}
